package com.orkun.api.converter.reader;

import java.io.DataInputStream;
import java.io.FileReader;
import java.util.ArrayList;

import au.com.bytecode.opencsv.CSVReader;

import com.orkun.api.converter.impl.AddressInfoModel;

/**
 * CSV reader implementation for AddressInfoModel
 * Ext lib ref: http://opencsv.sourceforge.net/
 */
public class ReaderCSV implements Reader<AddressInfoModel> {

  /*
   * Parses from CSV inputStream and returns an Arraylist. 
   * 
   * @see com.orkun.api.converter.reader.Reader#readData(java.io.DataInputStream)
   */
  public ArrayList<AddressInfoModel> readData(DataInputStream is) {
    ArrayList<AddressInfoModel> models = new ArrayList<>();
    try {

      @SuppressWarnings("resource")
      CSVReader reader = new CSVReader(new FileReader("res/sample_data.csv"), ',', '\'', 1);
      String[] nextLine;
      while ((nextLine = reader.readNext()) != null) {
        models.add(new AddressInfoModel(nextLine[0], nextLine[1], nextLine[2], nextLine[3]));
      }
    } catch (Exception e) {
      System.out.println("Exception occured while reading from CSV : " + e.getMessage());
      e.printStackTrace();
    }

    return models;
  }

}
